package com.qj.travel.alivod;

import com.aliyun.vod.client.*;
import com.aliyun.vod.common.*;
import com.qj.travel.constant.VodConstant;
import com.qj.travel.db.model.SnapInfoModel;
import com.qj.travel.db.model.TransInfoModel;
import com.qj.travel.db.model.VideoInfoModel;
import com.qj.travel.temp.TotalInfoTemp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Admin on 2016/9/29.
 */
public class VodTool {

    VODClient client = new DefaultVODClient(VodConstant.ACCESS_KEY_ID,VodConstant.ACCESS_KEY_SECRET);
//    public MediaWorkflowInfo getMediaWorkflowInfo() {
//        MediaWorkflowInfo mediaWorkflowInfo = client.getMediaWorkflowInfo(VodConstant.WORKFLOW_NAME);
//        return mediaWorkflowInfo;
//    }

    public TotalInfoTemp getTotalInfo() {

       // if (getMediaWorkflowInfo().state() == MediaWorkflowState.Active) {
        TotalInfoTemp totalInfoTemp = new TotalInfoTemp();
        List<MediaWorkflowMessage> mediaWorkflowMessageList = client.popMessages(VodConstant.MNS_ENDPOINT,VodConstant.QUEUE_NAME_IN,30); //接受工作流消息
            for (MediaWorkflowMessage mediaWorkflowMessage : mediaWorkflowMessageList) {
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Media media = client.getMedia(mediaWorkflowMessage.messageWorkflowName(),mediaWorkflowMessage.fileURL());
//               if (mediaWorkflowMessage.type() == MediaWorkflowMessageType.Start) {
   //             }
      //      if (mediaWorkflowMessage.type() == MediaWorkflowMessageType.Report) {
                    /**
                     * 获取输入媒体的信息
                     */
                    VideoInfoModel videoInfoModel = new VideoInfoModel();
                    String fileUrl = media.fileURL();
                    int width = media.mediaInfo().width();
                    int height = media.mediaInfo().height();
                    float duration = media.mediaInfo().duration();
                    String format = media.mediaInfo().format();
                    long fileSize = media.mediaInfo().fileSize();
                    float bitrate = media.mediaInfo().bitrate();
                    videoInfoModel.setFileUrl(fileUrl);
                    videoInfoModel.setWidth(width);
                    videoInfoModel.setHeight(height);
                    videoInfoModel.setDuration(duration);
                    videoInfoModel.setFormat(format);
                    videoInfoModel.setFileSize(fileSize);
                    videoInfoModel.setBitrate(bitrate);

                    totalInfoTemp.setVideoInfoModel(videoInfoModel);


                for (TranscodeOutput transcodeOutput : media.workflowExecutionOutputs().get(0).transcodeOutputs()) {
                        ActivityState activityState = transcodeOutput.state();
                        TransInfoModel transInfoModel = new TransInfoModel();
                        if (activityState == ActivityState.Success) {
                            String transOssUrl = transcodeOutput.ossUrl();
                            int transWidth = transcodeOutput.mediaInfo().width();
                            int transHeight = transcodeOutput.mediaInfo().height();
                            float transDuration = transcodeOutput.mediaInfo().duration();
                            String transFormat = transcodeOutput.mediaInfo().format();
                            long transFileSize = transcodeOutput.mediaInfo().fileSize();
                            float transBitrate = transcodeOutput.mediaInfo().bitrate();
                            transInfoModel.setTransOssUrl(transOssUrl);
                            transInfoModel.setTransWidth(transWidth);
                            transInfoModel.setTransHeight(transHeight);
                            transInfoModel.setTransDuration(transDuration);
                            transInfoModel.setTransFormat(transFormat);
                            transInfoModel.setTransFileSize(transFileSize);
                            transInfoModel.setTransBitrate(transBitrate);
                            totalInfoTemp.setTransInfoModel(transInfoModel);

                        } else if (activityState == ActivityState.Fail) {
                            FailedInfo failedInfo = transcodeOutput.failedInfo();
                            String transFailCode = failedInfo.getCode();
                            String transFailMessage = failedInfo.getMessage();
                            transInfoModel.setTransFailCode(transFailCode);
                            transInfoModel.setTransFailMessage(transFailMessage);
                            totalInfoTemp.setTransInfoModel(transInfoModel);
                        }

                    }

                    for (SnapshotOutput snapshotOutput : media.workflowExecutionOutputs().get(0).snapshotOutputs()) {
                        SnapInfoModel snapInfoModel = new SnapInfoModel();
                        ActivityState activityState = snapshotOutput.state();
                        if (activityState == ActivityState.Success) {
                            String snapOssUrl = snapshotOutput.ossUrl();
                            snapInfoModel.setSnapOssUrl(snapOssUrl);
                            totalInfoTemp.setSnapInfoModel(snapInfoModel);

                        } else if (activityState == ActivityState.Fail) {
                            FailedInfo failedInfo = snapshotOutput.failedInfo();
                            String snapFailCode = failedInfo.getCode();
                            String snapFailMessage = failedInfo.getMessage();
                            snapInfoModel.setSnapFailCode(snapFailCode);
                            snapInfoModel.setSnapFailMessage(snapFailMessage);
                            totalInfoTemp.setSnapInfoModel(snapInfoModel);
                        }
                    }

                List<String> receipHandles = new ArrayList<String>();
                receipHandles.add(mediaWorkflowMessage.receiptHandle());
                client.deleteQueueMessages(VodConstant.MNS_ENDPOINT,VodConstant.QUEUE_NAME_IN,receipHandles);

                }
//                List<String> receipHandles = new ArrayList<String>();
//                receipHandles.add(mediaWorkflowMessage.receiptHandle());
//                client.deleteQueueMessages(VodConstant.MNS_ENDPOINT,VodConstant.QUEUE_NAME_IN,receipHandles);
        //    }
//        } else if (getMediaWorkflowInfo().state() == MediaWorkflowState.Inactive) {
//            System.out.println("工作流处于非活跃状态！");
//        } else if (getMediaWorkflowInfo().state() == MediaWorkflowState.Deleted) {
//            System.out.println("工作流已被删除！");
//        }
        return totalInfoTemp;
    }

 /*   public TransInfoModel getTransInfoModel() {
       // if (getMediaWorkflowInfo().state() == MediaWorkflowState.Active) {
        List<MediaWorkflowMessage> mediaWorkflowMessageList = client.popMessages(VodConstant.MNS_ENDPOINT,VodConstant.QUEUE_NAME_IN,30);
        TransInfoModel transInfoModel = new TransInfoModel();
            for (MediaWorkflowMessage mediaWorkflowMessage : mediaWorkflowMessageList) {
                Media media = client.getMedia(mediaWorkflowMessage.messageWorkflowName(),mediaWorkflowMessage.fileURL());
                if (mediaWorkflowMessage.type() == MediaWorkflowMessageType.Start) {

                    for (TranscodeOutput transcodeOutput : media.workflowExecutionOutputs().get(0).transcodeOutputs()) {
                        ActivityState activityState = transcodeOutput.state();
                        if (activityState == ActivityState.Success) {
                            String transOssUrl = transcodeOutput.ossUrl();
                            int transWidth = transcodeOutput.mediaInfo().width();
                            int transHeight = transcodeOutput.mediaInfo().height();
                            float transDuration = transcodeOutput.mediaInfo().duration();
                            String transFormat = transcodeOutput.mediaInfo().format();
                            long transFileSize = transcodeOutput.mediaInfo().fileSize();
                            float transBitrate = transcodeOutput.mediaInfo().bitrate();
                            transInfoModel.setTransOssUrl(transOssUrl);
                            transInfoModel.setTransWidth(transWidth);
                            transInfoModel.setTransHeight(transHeight);
                            transInfoModel.setTransDuration(transDuration);
                            transInfoModel.setTransFormat(transFormat);
                            transInfoModel.setTransFileSize(transFileSize);
                            transInfoModel.setTransBitrate(transBitrate);

                        } else if (activityState == ActivityState.Fail) {
                            FailedInfo failedInfo = transcodeOutput.failedInfo();
                            String transFailCode = failedInfo.getCode();
                            String transFailMessage = failedInfo.getMessage();
                            transInfoModel.setTransFailCode(transFailCode);
                            transInfoModel.setTransFailMessage(transFailMessage);
                        }
                    }
                }

                if (mediaWorkflowMessage.type() == MediaWorkflowMessageType.Report) {


                }
//                List<String> receipHandles = new ArrayList<String>();
//                receipHandles.add(mediaWorkflowMessage.receiptHandle());
//                client.deleteQueueMessages(VodConstant.MNS_ENDPOINT,VodConstant.QUEUE_NAME_IN,receipHandles);
            }
    //    }
        return transInfoModel;
    }*/

/*    public SnapInfoModel getSnapInfoModel() {
    //    if (getMediaWorkflowInfo().state() == MediaWorkflowState.Active) {
        List<MediaWorkflowMessage> mediaWorkflowMessageList = client.popMessages(VodConstant.MNS_ENDPOINT,VodConstant.QUEUE_NAME_IN,30);
        SnapInfoModel snapInfoModel = new SnapInfoModel();
            for (MediaWorkflowMessage mediaWorkflowMessage : mediaWorkflowMessageList) {
                Media media = client.getMedia(mediaWorkflowMessage.messageWorkflowName(),mediaWorkflowMessage.fileURL());
                if (mediaWorkflowMessage.type() == MediaWorkflowMessageType.Start) {

                }

                if (mediaWorkflowMessage.type() == MediaWorkflowMessageType.Report) {

                    for (SnapshotOutput snapshotOutput : media.workflowExecutionOutputs().get(0).snapshotOutputs()) {
                        ActivityState activityState = snapshotOutput.state();
                        if (activityState == ActivityState.Success) {
                            String snapOssUrl = snapshotOutput.ossUrl();
                            snapInfoModel.setSnapOssUrl(snapOssUrl);

                        } else if (activityState == ActivityState.Fail) {
                            FailedInfo failedInfo = snapshotOutput.failedInfo();
                            String snapFailCode = failedInfo.getCode();
                            String snapFailMessage = failedInfo.getMessage();
                            snapInfoModel.setSnapFailCode(snapFailCode);
                            snapInfoModel.setSnapFailMessage(snapFailMessage);
                        }
                    }

                }
//                List<String> receipHandles = new ArrayList<String>();
//                receipHandles.add(mediaWorkflowMessage.receiptHandle());
//                client.deleteQueueMessages(VodConstant.MNS_ENDPOINT,VodConstant.QUEUE_NAME_IN,receipHandles);
            }
      //  }
        return snapInfoModel;
    }*/

 //   public void deleteQueueMessage() {
//        List<MediaWorkflowMessage> mediaWorkflowMessageList = client.popMessages(VodConstant.MNS_ENDPOINT,VodConstant.QUEUE_NAME_IN,30);
//        for (MediaWorkflowMessage mediaWorkflowMessage : mediaWorkflowMessageList) {
//            List<String> receipHandles = new ArrayList<String>();
//            receipHandles.add(mediaWorkflowMessage.receiptHandle());
//            client.deleteQueueMessages(VodConstant.MNS_ENDPOINT,VodConstant.QUEUE_NAME_IN,receipHandles);
//        }

//        for (MediaWorkflowMessage mediaWorkflowMessage : client.popMessages(VodConstant.MNS_ENDPOINT,VodConstant.QUEUE_NAME_IN,30)) {
//            List<String> receipHandles = new ArrayList<String>();
//            receipHandles.add(mediaWorkflowMessage.receiptHandle());
//            client.deleteQueueMessages(VodConstant.MNS_ENDPOINT,VodConstant.QUEUE_NAME_IN,receipHandles);
//        }
 //   }

}
